package cn.edu.ujn.lab3.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.ujn.lab3.dao.BaseDict;
import cn.edu.ujn.lab3.dao.BaseDictMapper;
import cn.edu.ujn.lab3.dao.Customer;
import cn.edu.ujn.lab3.dao.DictQueryVo;
import cn.edu.ujn.lab3.service.IBaseDictService;
import cn.edu.ujn.lab3.util.CheckName;
import cn.edu.ujn.lab3.util.dictEnableChange;
@Service("baseDictService")
@Transactional
public class BaseDictServiceImpl implements IBaseDictService{

	//注入BaseDictMapper成员
	@Autowired
	private BaseDictMapper baseDictDao;
	
	 //根据行业编号查找指定信息
	@Override
	@Transactional
	public List<BaseDict> selectByTypeCode(String code) {
		//调用BaseDictMapper中的根据行业编号查找指定信息方法
		List<BaseDict> list=baseDictDao.findDictByCode(code);
		return list;
	}

	@Override
	@Transactional
	public List<BaseDict> findDictByVo(DictQueryVo vo) {
		List<BaseDict> list = baseDictDao.findDictByVo(vo);
		return list;
	}  

	@Override
	@Transactional
	public Integer findDictByVoCount(DictQueryVo vo) {
		Integer count = baseDictDao.findDictByVoCount(vo);
		return count;
	}

	@Override
	@Transactional
	public List<dictEnableChange> createChangeList() {
		// TODO Auto-generated method stub
		dictEnableChange ec1=new dictEnableChange();
		dictEnableChange ec2=new dictEnableChange();
		List<dictEnableChange> ecList =new ArrayList<dictEnableChange>();
		ec1.setNumber("1");
		ec1.setState("可用");
		ecList.add(ec1);
		ec2.setNumber("0");
		ec2.setState("不可用");
		ecList.add(ec2);
		return ecList;
	}

	@Override
	@Transactional
	public int upBaseDictById(BaseDict record) {
		// TODO Auto-generated method stub
		return baseDictDao.updateByPrimaryKeySelective(record);	
	}

	@Override
	@Transactional
	//进行输入信息检验
	public boolean checkBaseDict(BaseDict record) {
		// TODO Auto-generated method stub
		//检验信息的正确性
		if(!CheckName.validateName(record.getDictItemName()) ||
		   
		   "".equals(record.getDictEnable()) 
		  
		   )
		return false;
		return true;
	}

	@Override
	@Transactional
	//通过Id查找字典
	public BaseDict getBaseDictById(String dictId) {
		// TODO Auto-generated method stub
		return baseDictDao.selectByPrimaryKey(dictId);
	}

	@Override
	@Transactional
	//状态反转
	public int updateEnableByPrimaryKey(String dictId) {
		//调用BaseDictMapper中的根据字典Id查找指定信息方法
		BaseDict dict=baseDictDao.selectByPrimaryKey(dictId);
		//判断dictEnable是1还是0
		if("1".equals(dict.getDictEnable())) {
           
			return baseDictDao.updateEnable0ByPrimaryKey(dictId);
			
		}
		else {
			return baseDictDao.updateEnable1ByPrimaryKey(dictId);
		}
		
	}

	
	//获取查询到的dictType名称
		@Override
		@Transactional
		public List<BaseDict> findDictType() {
			List<BaseDict> list=baseDictDao.findDictType();
			return list;
		}
	
	//插入函数
	@Override
	@Transactional
	public int insert(BaseDict dictId) {
		dictId.setDictEnable("1");
		dictId.setDictMemo(null);
		dictId.setDictItemCode(null);
		String MaxMoreId = maxDictID();
		dictId.setDictId(MaxMoreId);
		String getTypeName = dictId.getDictTypeName();
		int maxDictSort = maxDictSort(getTypeName);
		dictId.setDictSort(maxDictSort);
		String code = selectByTypeName(getTypeName);
		if(code!=null) {
			dictId.setDictTypeCode(code);
		}
		else {
			String maxMore = maxTypeCode();	
			dictId.setDictTypeCode(maxMore);
		}
		int back = baseDictDao.insert(dictId);
		return back;
	}

	//查询输入类型名称是否存在
	@Override
	@Transactional
	public String selectByTypeName(String dictTypeName) {
		List<BaseDict> dict = baseDictDao.selectByTypeName(dictTypeName);
		if(dict!=null&&dict.size()!=0) {
			BaseDict  dictFirst= (BaseDict)dict.get(0);
			String count = dictFirst.getDictTypeCode();
			return count;
		}
		else {
			return null;
		}
		
	}
	//找出类型代码的最大值并加一
	@Override
	@Transactional
	public String maxTypeCode() {
		List<BaseDict> allTypeCodeStirng = baseDictDao.findDictType();
		Integer max = 1;
		
		for(int i=0;i<allTypeCodeStirng.size();i++) {
			BaseDict dict = (BaseDict)allTypeCodeStirng.get(i);
			Integer compare = Integer.parseInt(dict.getDictTypeCode());
			if(compare>max) {
				max = compare;
			}
		}
		Integer maxMoreI = max+1;
		String maxMoreS = (String.format("%03d",maxMoreI)).toString();
		return maxMoreS;
	}
	//找出ID的最大值并加一
	@Override
	@Transactional
	public String maxDictID() {
		List<BaseDict> allString = baseDictDao.selectAll();
		BaseDict dictFirst = (BaseDict)allString.get(0);
		Integer maxID = Integer.parseInt(dictFirst.getDictId());	
		
		for(int i=0;i<allString.size();i++) {
			BaseDict dict = (BaseDict)allString.get(i);
			Integer compare = Integer.parseInt(dict.getDictId());
			if(compare>maxID) {
				maxID = compare;
			}
		}
		Integer maxMoreI = maxID +1;
		String maxMoreS = maxMoreI.toString();
		return maxMoreS;
	}
	//找出同类型的序号
	@Override
	@Transactional
	public int maxDictSort(String dictTypeName) {
		List<BaseDict> dict = baseDictDao.selectByTypeName(dictTypeName);
		if(dict!=null&&dict.size()!=0) {
			return (dict.size()+1);
		}
		else {
			return 1;
		}
	}
}
